package com.campsys.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campsys.entity.Student;
import com.campsys.entity.User;
import com.campsys.entity.dto.StudentDto;
import com.campsys.mapper.StudentMapper;
import com.campsys.mapper.UserMapper;
import com.campsys.service.StudentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
    @Resource
    private UserMapper userMapper;

    @Override
    public boolean createStudent(Student student) {
        User user = new User();
        user.setLoginName(student.getStuName());
        user.setPassword("123456");
        user.setUserIdentity("3");
        userMapper.insertUser(user);
        student.setLoginId(user.getId());
        return save(student)?true:false;
    }

    @Override
    public boolean importStudents(List<StudentDto> studentsDto) {
        for (StudentDto studentDto : studentsDto) {
            // Convert StudentDto to Student entity
            Student student = convertToStudentEntity(studentDto);

            User user = new User();
            user.setLoginName(student.getStuName());
            user.setPassword("123456");
            user.setUserIdentity("3");
            userMapper.insertUser(user);
            student.setLoginId(user.getId());

            save(student);
        }
        return true;
    }

    @Override
    public Student convertToStudentEntity(StudentDto studentDto) {
        Student student = new Student();
        student.setStuName(studentDto.getStuName());
        student.setFromPlace(studentDto.getFromPlace());
        student.setParentPhone(studentDto.getParentPhone());
        student.setCurrentGrade(studentDto.getCurrentGrade());
        student.setIsFirst(studentDto.getIsFirst());
        student.setComment(studentDto.getComment());
        student.setIsAudit(studentDto.getIsAudit());
        return student;
    }


}
